package com.zone.lib.download.execute;

import android.content.Intent;
import android.util.Log;

import com.zone.lib.download.enums.AppStatusEnum;
import com.zone.lib.download.DownloadConstant;
import com.zone.lib.download.utils.LogUtils;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DownloadExecutor extends ThreadPoolExecutor{

    public static final String TAG = DownloadExecutor.class.getName();

    public DownloadExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
                            TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public void executeTask(DownloadTask task){
        int status = task.getStatus();
        Log.d(TAG, "download task status = "+status);
        // TODO 无限测试 暂时注销
//        if (status == AppStatusEnum.DOWNLOAD_WAIT.CODE
//            || status == AppStatusEnum.DOWNLOAD_PREPARE.CODE
//                || status == AppStatusEnum.DOWNLOAD_PAUSE.CODE
//                    || status == AppStatusEnum.DOWNLOAD_FAIL.CODE){
            Intent intent = new Intent();
            intent.setAction(DownloadConstant.APP_STATUS_ACTION);
            intent.putExtra(DownloadConstant.EXTRA_INTENT_APP_STATUS, task.getDownload());
            task.sendBroadcast(intent);
            execute(task);
//        }else {
//            LogUtils.w(TAG, "文件状态不正确, 不进行下载 FileInfo=" + task.getDownload().toString());
//        }
    }
}
